package com.ulook.lookplanet.ui.square.pub;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.ulook.lookplanet.base.BaseViewModel2;
import com.ulook.lookplanet.common.AppServer;
import com.ulook.lookplanet.data.model.TArtPic;
import com.ulook.lookplanet.data.model.TArtTopic;
import com.ulook.lookplanet.data.model.TArticle;
import com.ulook.lookplanet.data.model.TCommunity;
import com.ulook.lookplanet.data.model.TTopic;
import com.ulook.lookplanet.data.repository.PubRepository;
import com.ulook.lookplanet.util.json.JacksonUtil;
import com.ulook.lookplanet.util.net.AsynRequestUtil;
import com.ulook.lookplanet.util.net.OkHttpException;
import com.ulook.lookplanet.util.net.RequestParams;
import com.ulook.lookplanet.util.net.ResponseCallback;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * 描述：
 *
 * @author L1998
 * @data 2021/4/6
 */
public class PubViewModel extends BaseViewModel2 {

    private PubRepository repository;
    private MutableLiveData<PubState> pubState = new MutableLiveData<>();
    private String uid;
    private List<TCommunity> communities; // 后端获取的分组列表
    private List<TTopic> topics; // 后端获取的话题列表
    private TCommunity chooseCom;  // 用户选择的分组
    private List<TArtTopic> artTopics = new ArrayList<>(); // 用户选择的话题

    public PubViewModel() {
        repository = PubRepository.getInstance();
        uid = UUID.randomUUID().toString();
        getGroups();
        getTopics("0");
    }

    public String getUid() {
        return uid;
    }

    public LiveData<PubState> getPubState() {
        return pubState;
    }

    public List<TCommunity> getCommunities() {
        return communities;
    }

    public TCommunity getChooseCom() {
        return chooseCom;
    }

    public void setChooseCom(TCommunity community) {
        // 设置选择的分组
        this.chooseCom = community;
        // 获取话题
        getTopics(community.getUid());
    }

    public void addTopic(TArtTopic tArtTopic){
        artTopics.add(tArtTopic);
    }

    public List<TArtTopic> getArtTopics(){
        return artTopics;
    }

    public List<TTopic> getTopics() {
        return topics;
    }

    /**
     * 发贴
     */
    public void pubArticle(PubArticleInfo pubArticleInfo) {

        ResponseCallback callback = new ResponseCallback() {
            @Override
            public void onSuccess(Object res) {
                if(artTopics.size() > 0){
                    for(TArtTopic t : artTopics){
                        RequestParams params = new RequestParams();
                        params.put("artId", uid);
                        params.put("topicId", t.getTopicid());
                        AsynRequestUtil.post(AppServer.ROOT_PATH + "/pub/addArtTopic", params, null, null);
                    }
                }
                pubState.setValue(new PubState(0));
                setToastText("发布成功");
            }

            @Override
            public void onFailure(OkHttpException failuer) {
                pubState.setValue(new PubState(0, 1));
                setToastText("发布失败");
            }
        };
        pubState.setValue(new PubState(1, 0));
        repository.pub(pubArticleInfo, callback);
    }

    /**
     * 获取分组
     */
    private void getGroups(){
        ResponseCallback callback = new ResponseCallback() {
            @Override
            public void onSuccess(Object res) {
                try {
                    communities = JacksonUtil.json2list((String)res, TCommunity.class);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void onFailure(OkHttpException failuer) {

            }
        };
        AsynRequestUtil.post(AppServer.ROOT_PATH + "/pub/getComList", null, null, callback);
    }

    /**
     * 获取话题
     */
    private void getTopics(String comId){
        ResponseCallback callback = new ResponseCallback() {
            @Override
            public void onSuccess(Object res) {
                try {
                    topics = JacksonUtil.json2list((String)res, TTopic.class);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void onFailure(OkHttpException failuer) {

            }
        };
        RequestParams params = new RequestParams();
        params.put("comId", comId);
        AsynRequestUtil.post(AppServer.ROOT_PATH + "/pub/getTopicByComId", params, null, callback);
    }

}
